<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="__STATIC__/plugins/layui/css/layui.css" media="all">
</head>
<body>
<script src="__STATIC__/plugins/layui/layui.js"></script>
<form method="POST" action="" accept-charset="UTF-8" class="layui-form">
    <input type="hidden" name="qids" id="npm">
    <div class="layui-form-item"><label for="name" class="layui-form-label">问卷名称</label>
        <div class="layui-input-block"><input class="layui-input" readonly type="text" value="{$list.name}"  lay-verify="required"></div>
    </div>



    <div class="layui-form-item" >
        <table id="demo" lay-filter="demo"></table>
    </div>

    <!---->
    <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
        <button class="layui-btn layui-btn-primary" type="reset"><a class="layui-layer-btn1">取消</a></button>
    </div>
</form>
</body>
<script>
    layui.use(['form','table','layer'], function () {
         var form = layui.form;
         var layer=layui.layer;
         var $ = layui.$;
        var table=layui.table;

      form.on('submit(formDemo)',function(data){
          var ids=localStorage.getItem('ids');
          $.post("{:url('questionnaire/edit')}",{qids:ids,id:'{$list.id}'},function(response){
              layer.msg(response.msg);
              if(response.code==0){
                  var index=parent.layer.getFrameIndex(window.name);
                  setTimeout(function(){
                      parent.layer.close(index);
                  },1000)
              }
          });
          localStorage.removeItem('ids');
          return false;
      });

        table.render({
            id:'demo',
            elem: '#demo'
            ,height: 315
            ,url: '{:url("question/questionSelect")}' //数据接口
            ,where:{id:{$list.id}}
            ,cols: [[ //表头
                {checkbox: true, fixed: true}
                ,{field: 'name', title: '问题名称', width:200}
                ,{field: 'type', title: '类型', width:'auth', sort: true}
            ]]
            ,done:function(res){
                var init=[];
                res.data.forEach(function(v){
                    if(v.LAY_CHECKED){
                        init.push(v.id);
                    }
                });
                console.log(init);
                localStorage.setItem('ids',init.join(','));
            }
        });
        table.on('checkbox(demo)',function(obj){
            if(obj.type=='all'){
                ids=[];
                if(obj.checked){
                    var allData=table.cache.demo;
                    allData.forEach(function(v,i){
                        ids.push(v.id);
                    });
                }
            }else{
                console.log(obj.data.id);
                var ids=localStorage.getItem('ids');
                console.log(obj);
                ids=(ids)?ids.split(','):[];
                if(obj.checked){
                    ids.push(obj.data.id);
                }else{
                    var index=ids.indexOf(obj.data.id);
                    ids.splice(index,1);
                }
            }
            localStorage.setItem('ids',ids.join(','));
         });
    });
</script>
</html>;